﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE_TOOL.DBAccess
{
	class ExchangeRateDAO : BaseDAO
	{
		#region Basic Method

		public static DataTable GetAll()
		{
            return ExcuteProcedure(ToolConstants.StoreProcedure.Rate.GetAll);
		}

		public static DataTable GetOne(int exchangeRateId)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@ID", SqlDbType.Int, exchangeRateId)
                    };
                return ExcuteProcedure(ToolConstants.StoreProcedure.Rate.GetOne, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		public static ExchangeRates LoadOne(int exchangeRateId)
		{
			ExchangeRates o = new ExchangeRates();
			DataTable dt = GetOne(exchangeRateId);
			if(dt.Rows.Count > 0)
			{
				o.ID = int.Parse(dt.Rows[0][ToolConstants.Entities.ExchangeRate.FieldName.ID].ToString());
				o.Rate = decimal.Parse(dt.Rows[0][ToolConstants.Entities.ExchangeRate.FieldName.Rate].ToString());
				o.Date = DateTime.Parse(dt.Rows[0][ToolConstants.Entities.ExchangeRate.FieldName.Date].ToString());

			}

			return o;
		}


		public static int Insert(ExchangeRates o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        //CreateParameter("@ID", SqlDbType.Int, o.ID),
                        CreateParameter("@Rate", SqlDbType.Decimal, o.Rate),
                        //CreateParameter("@Date", SqlDbType.DateTime, o.Date),
                    };

                return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Rate.Insert, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		public static int Update(ExchangeRates o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@ID", SqlDbType.Int, o.ID),
                        CreateParameter("@Rate", SqlDbType.Decimal, o.Rate),
                        //CreateParameter("@Date", SqlDbType.DateTime, o.Date),
                    };

                return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Rate.Update, parameters);
			}
			catch(Exception e)
			{
				throw new MyException(e);
			}
		}

		public static int Delete(ExchangeRates o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@ID", SqlDbType.NVarChar, o.ID),
                    };

                return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Rate.Delete, parameters);
			}
			catch(Exception e)
			{
				throw new MyException(e);
			}
		}

		public static DataTable GetTemplateTable()
		{
			DataTable table = new DataTable("333_Rate");

			return table;
		}

		#endregion
	}
}
